"use strict";
import * as React from 'react';
import { commonProps, commonStyles } from "../../assets/styles/common";
import NavBackButton from "../../component/BackButton";
const { View, Alert, Platform, FlatList, TouchableOpacity } = require('react-native');
const { connect } = require('react-redux');
const NavigationBar = require('react-native-navbar').default;
const { Actions } = require("react-native-router-flux");
const Contacts = require('react-native-contacts');
import MyText from "../../component/MyText";
import MyIcon from "../../component/icon";
import Request from "../../utils/Request";
class ContactSelectPage extends React.Component {
    constructor() {
        super(...arguments);
        this.state = {
            contacts: []
        };
        this.getAll = () => {
            Contacts.getAll((err, contacts) => {
                Request.log('获取所有联系人', contacts);
                this.setState({ contacts });
            });
        };
        this.alertNoPermission = () => {
            Alert.alert('警告', '暂无通讯录权限,请前往设置->隐私->通讯录->火星空间 打开通讯录权限', [
                { text: '取消' },
                { text: '确定' },
            ]);
        };
        this.onSelect = (c) => {
            if (this.props.onSelect) {
                this.props.onSelect(c);
            }
            Actions.pop();
        };
        this.renderItem = (p) => {
            const c = p.item;
            let text = c.familyName + c.givenName + ' ' + c.company;
            return (<TouchableOpacity onPress={this.onSelect.bind(this, c)} style={[commonStyles.listItem, commonStyles.underLine, {
                    justifyContent: 'space-between',
                    padding: 10
                }]}>
                <MyText>{text}</MyText>
                <MyIcon name={'icon-iconfontyoujiantou'}/>
            </TouchableOpacity>);
        };
    }
    componentDidMount() {
        Contacts.checkPermission((err, permission) => {
            if (permission === 'undefined') {
                Contacts.requestPermission((err, permission) => {
                    if (permission === 'authorized') {
                        this.getAll();
                    }
                    else {
                        Actions.pop();
                    }
                });
            }
            else {
                if (permission !== 'authorized') {
                    this.alertNoPermission();
                }
                else {
                    this.getAll();
                }
            }
        });
    }
    render() {
        return (<View style={commonStyles.container}>
                <NavigationBar {...commonProps.navBar} leftButton={<NavBackButton />} title={{ title: '选择联系人', tintColor: '#fff' }}/>
                <FlatList data={this.state.contacts} renderItem={this.renderItem} keyExtrator={(_, index) => index}/>
            </View>);
    }
}
export default ContactSelectPage;
